<?php

namespace app\weapp\model;

use think\Model;

/**
 * Activity
 */
class Activity extends Model
{
    // 表名
    protected $name = 'activity';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'city_text','image_url'
    ];
    public static function onAfterRead($activity){
        if($activity['process']>0){
            if(strtotime($activity['start_time'] )<=time() && strtotime($activity['end_time'])>=time()){
            $activity->process = 1;
            $activity->save();
            }
            if(strtotime($activity['end_time'])<time()){
                $activity->process = 0;
                $activity->save();
            }
        }
        
    }

    public function getCityAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function getImageUrlAttr($value,$row):string
    {
        if($row['image'] !== ''){
            return full_url($row['image']);
        }
        return '';
    }
    public function getCityTextAttr($value, $row): string
    {
        if ($row['city'] === '' || $row['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->column('name');
        return $cityNames ? implode('', $cityNames) : '';
    }

    public function getContentAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function getSiteAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }



    
}